//
//  PaperSearchModel.m
//  智云题库
//
//  Created by 毕武侠 on 2017/3/23.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperSearchModel.h"

@implementation PaperSearchModel

- (void)mj_keyValuesDidFinishConvertingToObject {
    NSString *str = [NSString stringWithFormat:@"【%@】%@", self.ques_model == 0 || self.ques_model == 2 ? @"单选题" : @"多选题", self.ques_stem_text];
    self.ques_text = [self attributedStringWith:str withRegexStr:self.keywords];
}

- (NSAttributedString *)attributedStringWith:(NSString *)string withRegexStr:(NSString *)regexStr {
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:string];
    [attriString addAttribute:NSForegroundColorAttributeName value:MainThemeColor range:NSMakeRange(0, 5)];
    
    NSString *pattern = [NSString stringWithFormat:@"[%@]", regexStr];
    NSError *error = nil;
    
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
    NSArray<NSTextCheckingResult *> *result = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
    if (result) {
        for (int i = 0; i<result.count; i++) {
            NSTextCheckingResult *res = result[i];
            [attriString addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:res.range];
        }
    }
    return attriString;
}

@end
